package org.radigan.ycoa.scan.impl

import org.radigan.ycoa.scan.service.ScanService

import java.util.Properties
import org.osgi.framework.BundleActivator
import org.osgi.framework.BundleContext
import org.osgi.framework.ServiceListener
import org.osgi.framework.ServiceEvent
import org.osgi.framework.ServiceRegistration
import org.osgi.framework.ServiceReference
import org.osgi.service.cm.ConfigurationAdmin
import org.osgi.service.cm.Configuration

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

public class ScanServiceImpl implements ScanService, BundleActivator {

  private static final transient Log log = LogFactory.getLog(ScanServiceImpl.class)
  private ServiceRegistration registration

  public void start(BundleContext context) {
    String propertiesId = "ycoa.scan"
    ServiceReference serviceReference = context.getServiceReference(ConfigurationAdmin.class.getName())
    ConfigurationAdmin configurationAdmin = (ConfigurationAdmin)context.getService(serviceReference)
    Configuration configuration = configurationAdmin.getConfiguration(propertiesId, null)
    Dictionary<Object, Object> properties = configuration.getProperties()
    if(!properties) throw new Exception("""No such properties ID "${propertiesId}".""")
    log.info("Test property - markets := ${properties.get('markets')}")
    log.info("Test property - cron := ${properties.get('cron')}")
    Properties props = new Properties()
    props.put("key1", "value1")
    registration = context.registerService(ScanService.class.getName(), this, props)
  }

  public void stop(BundleContext context) {
  }

  public void scan() {
    println "scanning..."
  }

}
